<?php
namespace app\data_magic\logic;

use think\db\Query;


/**
 * 加载Scene的逻辑
 */
class Scene extends Structure
{
    public $tableName="";
    public $structureId=null;
    public $auth="";
    public $filter="";

    public $primary_key="";
    public $user_id_field="";
    public $create_time="";
    public $update_time="";

    public $meta=null;
    public $fields=null;
    public function getDefinition()
    {
        return [
            "meta"=>$this->meta,
            "fields"=>$this->fields,
        ];
    }
    /**
     * 加载
     * @param int $id 场景ID
     * @return array
     */
    public function init($id)
    {
        $scene=$this->getDatabase()->table("dm_scene")->where(["id"=>$id])->find();
        $structure=$this->getStructureDetail($scene["table_id"]);
        $table=$structure["table"];
        $fields=$structure["fields"];

        //处理字段的可读、可写、必填属性
        $readable=explode(",",$scene["readable"]);
        $writable=explode(",",$scene["writable"]);
        $required=explode(",",$scene["required"]);
        foreach($fields as $index => $field){
            $field_id=$field["id"];
            $field["readable"]=in_array($field_id,$readable);
            $field["writable"]=in_array($field_id,$writable);
            $field["required"]=in_array($field_id,$required);
            $fields[$index]=$field;

            if($field_id===$table["primary_key_field"]){
                $this->primary_key=$field["name"];
            }
            if($field_id===$table["user_id_field"]){
                $this->user_id_field=$field["name"];
            }
            if($field_id===$table["create_time_field"]){
                $this->create_time=$field["name"];
            }
            if($field_id===$table["update_time_field"]){
                $this->update_time=$field["name"];
            }
        }
        //将$table、$scene中的信息整合到$meta
        $meta=$table;
        $meta["id"]=$scene["id"];
        if($scene["name"]){
            $meta["name"]=$scene["name"];
        }
        if($scene["title"]){
            $meta["title"]=$scene["title"];
        }
        if($scene["describe"]){
            $meta["describe"]=$scene["describe"];
        }
        //设置过滤数据的条件
        if($scene["params"]){
            $param_list=explode(";",$scene["params"]);
            foreach($param_list as $index => $param){
                if(empty($param)){
                    unset($param_list[$index]);
                }
            }
            if(count($param_list)>0){
                $this->filter="(".join(") AND (",$param_list).")";
            }
        }

        $this->meta=$meta;
        $this->fields=$fields;
        $this->tableName=$table["name"];
        $this->structureId=$scene["table_id"];
        $this->auth=$scene["auth"];
        return $this->getDefinition();
    }
    /**
     * 以结构为基础加载
     * @param int $id 结构ID
     * @return array
     */
    public function initWithStructure($id)
    {
        $structure=$this->getStructureDetail($id);
        $this->meta=$structure["table"];
        $this->fields=$structure["fields"];
        $this->tableName=$structure["table"]["name"];
        $this->structureId=$id;
        return $this->getDefinition();
    }
    /**
     * 以数据表为基础加载
     * @param string $table_name 数据表名称
     * @return array
     */
    public function initWithDatabaseTable($table_name)
    {
        $structure=$this->transDatabaseTableToStructure($table_name);
        $this->meta=$structure["table"];
        $this->fields=$structure["fields"];
        $this->tableName=$table_name;
        return $this->getDefinition();
    }
    /**
     * 获取要查询的数据表
     * @return Query
     */
    public function getQuery()
    {
        $query=$this->getDatabase()->table($this->tableName);
        if($this->filter){
            $query->where($this->filter);
        }
        return $query;
    }
    /**
     * 增加
     */
    public function createRecord($data){
        $fields=[];
        foreach($this->fields as $item){
            $fields[]=$item["name"];
        }
        $current_time=time();
        $data[$this->create_time]=$current_time;
        $data[$this->update_time]=$current_time;
        return $this->getQuery()->strict(false)->field($fields)->insert($data);
    }
    /**
     * 删除
     * @param int $pks 主键ID 允许多个，用","分隔
     * @return bool
     */
    public function removeRecords($pks="")
    {
        return $this->getQuery()->where($this->primary_key." in ($pks)")->delete();
    }
    /**
     * 设置单行数据
     * @param array $data 数据
     * @param int $pk 主键ID
     * @return bool|int
     */
    public function updateRecord($pk,$data)
    {
        $fields=[];
        foreach($this->fields as $item){
            $fields[]=$item["name"];
        }
        $data[$this->update_time]=time();
        return $this->getQuery()->where($this->primary_key."=".$pk)->strict(false)->field($fields)->update($data);
    }
    /**
     * 读取多行数据
     * @return array
     */
    public function searchRecords()
    {
        return $this->getQuery()->select();
    }
    /**
     * 读取单行数据
     * @param int $pk 主键ID
     * @return array
     */
    public function getRecord($pk)
    {
        return $this->getQuery()->where($this->primary_key."=".$pk)->find();
    }
}
